<template>
	<view class="">
		<!-- 
			1.type:  group社群  
			groupPrice  discount_price  优惠券金额	
						
			2.type: activity活动
			iSVip 是否会员    vipPrice    normalPrice    discount_price  优惠券金额		 
		-->
		<u-popup round="30rpx" :show="btmShow" mode="bottom" @close="closePop" @open="">
			<view class="btmPop_wrapper">
				<!-- {{JSON.stringify(dataDic)}} -->
				<view class="name" v-if="dataDic.type == 'activity'">
					<text>¥{{payMoney}}</text>
					<!-- <text class="activityTitle">{{dataDic.title}}</text> -->
				</view>
				<view class="activityTitle" v-if="dataDic.type == 'activity'"> {{dataDic.title}} </view>

				<view class="type1" v-if="calculatPrice > 0">
					<view class="left">
						<image class="img" src="@/static/images/BX/payPic4.png"></image>
						<view class="title">会员优惠</view>
					</view>
					<view class="right">
						<text class="title">-¥{{calculatPrice}}</text>

					</view>
				</view>


				<!--  -->
				<view class="type1" @click="couponClick">
					<view class="left">
						<image class="img" src="@/static/images/BX/payPic1.png"></image>
						<view class="title">优惠券</view>
					</view>
					<view class="scoreRight">
						<view class="scoreTitle1" v-if="couponItem">¥{{couponItem.money}}</view>
						<view class="scoreTitle1" v-else>查看</view>
					</view>
					<image class="jian arrowTransformReturn" mode="widthFix" src="/static/images/BX/payPic3.png"></image>
				</view>


				<template v-if="!hiddenScore">
					<view class="type1" @click="scoreClick" v-if="showScoreItem">
						<view class="left">
							<image class="img" src="@/static/images/BX/payPic2.png"></image>
							<view class="title">账户积分</view>
						</view>
						<view class="scoreRight" v-if="scoreTF > 0">
							<view class="scoreTitle1">-¥{{scoreToPrice}}</view>
							<view class="scoreTitle2">{{scoreTF}}积分抵扣</view>
						</view>
						<image class="jian" :class="{ 'arrowTransform': showScore==true, 'arrowTransformReturn': showScore==false}"
							mode="widthFix" src="/static/images/BX/payPic3.png"></image>
					</view>

					<view style="height: 20rpx;background: #F5F6FA;;"></view>
					<view class="type1" v-if="showScore">
						<view class="TF">
							<input v-model="scoreTF" type="number" @blur='blur' placeholder="请输入使用积分" />
						</view>
					</view>
					<view class="type1 typewarn">
						<view class="warnAlert">账户总积分{{coinDic.coin_now}}，10积分=1元</view>
					</view>
				</template>








				<view class="payBtn" @click="payClick">确认付款</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				scoreTF: '',
				btmShow: false,
				dataDic: {
					title: '',
					type: '',
					groupPrice: '',
					discount_price: '',
					iSVip: '',
					vipPrice: '',
					normalPrice: '',
					discount_price: '',
					itemDic: {}, //传递过来的上级页面整体的对象，后续拓展使用
				},
				showScoreItem: true, //是否展示账户积分的条目， 优惠券>金额的时候不显示  全部抵扣了
				showScore: true, //是否展示积分
				maxScore: '', //限制到的最高可以使用的积分
				useScore: '', //可以使用的积分金额上线
				coinDic: {},
				couponItem: null,
				hiddenScore: false
			}
		},
		onLoad() {

		},
		props: {

		},
		computed: {
			payMoney() {
				// iSVip 是否会员    vipPrice    normalPrice    discount_price  优惠券金额		 
				if (this.dataDic.iSVip) { //VIP不能使用会员优惠 现在改成可以使用优惠券了

					let vipPrice = parseFloat(this.dataDic.vipPrice)
					let scorePrice = 0

					if (this.scoreTF.length > 0) {
						scorePrice = parseInt(this.scoreTF) / 10
					}
					//
					if (this.couponItem) {
						//优惠券的金额
						let couponMoney = parseFloat(this.couponItem.money)
						let realPay = vipPrice - parseFloat(this.calculatPrice) - couponMoney - scorePrice
						if (realPay < 0) {
							realPay = 0
						}
						return realPay.toFixed(1)
					} else {
						let realPay = vipPrice - parseFloat(this.calculatPrice) - scorePrice
						if (realPay < 0) {
							realPay = 0
						}
						return realPay.toFixed(1)
					}
				} else {
					let normalPrice = parseFloat(this.dataDic.normalPrice)

					let scorePrice = 0
					if (this.scoreTF.length > 0) {
						scorePrice = parseInt(this.scoreTF) / 10
					}
					// calculatPrice 优惠券展示的价格，这个根据会员不通展示的金额不通
					if (this.couponItem) {

						//优惠券的金额
						let couponMoney = parseFloat(this.couponItem.money)
						let realPay = normalPrice - parseFloat(this.calculatPrice) - couponMoney - scorePrice
						if (realPay < 0) {
							realPay = 0
						}
						return realPay.toFixed(1)

					} else {
						let realPay = normalPrice - parseFloat(this.calculatPrice) - scorePrice
						if (realPay < 0) {
							realPay = 0
						}
						return realPay.toFixed(1)
					}



				}
			},
			// 优惠券需要展示的价格  动态计算有会员金额
			calculatPrice() {
				let discount_price1 = parseFloat(this.dataDic.discount_price)
				console.log(this.dataDic.discount_price, 'this.dataDic.discount_price')
				return discount_price1.toFixed(0)

				/*
					type: activity活动
					iSVip 是否会员    vipPrice    normalPrice    discount_price  优惠券金额					
				*/
				let vipPrice = parseFloat(this.dataDic.vipPrice)
				let normalPrice = parseFloat(this.dataDic.normalPrice)
				let discount_price = parseFloat(this.dataDic.discount_price)
				if ((normalPrice - vipPrice) >= discount_price) {
					return discount_price
				} else {
					let maxChaJia = normalPrice - vipPrice
					return maxChaJia
				}
			},
			scoreToPrice() {
				let toMoney = parseInt(this.scoreTF) / 10
				console.log(toMoney)
				return toMoney
			}
		},
		methods: {
			couponClick() {
				let _that = this
				// 三个价格
				let shengyuPrice1 = 0

				let vipPrice1 = parseFloat(_that.dataDic.vipPrice)
				let normalPrice1 = parseFloat(_that.dataDic.normalPrice)
				if (_that.dataDic.iSVip) { //VIP不能使用会员优惠 现在改成可以使用优惠券了
					shengyuPrice1 = vipPrice1 - parseFloat(_that.calculatPrice)
				} else {
					shengyuPrice1 = normalPrice1 - parseFloat(_that.calculatPrice)

				}
				shengyuPrice1 = shengyuPrice1.toFixed(2)
				uni.navigateTo({
					url: '/BX/home/coupon' + `?price=${shengyuPrice1}`,
					events: {
						// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
						couponData: function(res) {
							console.log(res.data)
							_that.couponItem = res.data
							_that.scoreTF = 0


							// 三个价格
							let vipPrice = parseFloat(_that.dataDic.vipPrice)

							let normalPrice = parseFloat(_that.dataDic.normalPrice)
							let discount_price = parseFloat(_that.dataDic.discount_price)

							//优惠券展示的金额
							let couponMoney = parseFloat(_that.couponItem.money)


							// 内部成员   会员价格展示金额  不显示优惠券  可以使用积分
							// 正常用户   原价展示金额  显示优惠券   可以使用积分  优惠券>两个价格差，显示价格差   小于的话<=两个价格差, 显示优惠券金额

							if (_that.dataDic.iSVip) { //VIP不能使用会员优惠
								let couponPrice = _that.calculatPrice
								let shengyuPrice = (vipPrice - couponPrice - couponMoney).toFixed(2)

								if (shengyuPrice == 0 || shengyuPrice < 0) {
									_that.hiddenScore = true
								} else {
									_that.hiddenScore = false
								}
								console.log(shengyuPrice)
								let shengyuScore = (shengyuPrice * 10) //剩余的金额对应的积分

								// 输入完成后最高限制的积分
								if (shengyuScore > _that.coinDic.coin_now) {
									_that.maxScore = _that.coinDic.coin_now
									debugger
								} else {
									_that.maxScore = shengyuScore
									debugger
								}
								// _that.scoreTF = _that.maxScore.toString() //智能匹配为最高可以使用的积分  该功能取消了

							} else { //非会员
								console.log(typeof(_that.calculatPrice))
								let couponPrice = _that.calculatPrice
								let shengyuPrice = (normalPrice - couponPrice - couponMoney).toFixed(2)
								console.log(shengyuPrice)

								if (shengyuPrice == 0 || shengyuPrice < 0) {
									_that.hiddenScore = true
								} else {
									_that.hiddenScore = false
								}


								let shengyuScore = (shengyuPrice * 10) //剩余的金额对应的积分
								console.log(shengyuScore)

								// 输入完成后最高限制的积分							
								if (shengyuScore > _that.coinDic.coin_now) {
									_that.maxScore = _that.coinDic.coin_now
									debugger
								} else {
									_that.maxScore = shengyuScore
									debugger
								}
								// _that.scoreTF = _that.maxScore.toString() //智能匹配为最高可以使用的积分
							}




						}

					}
				})
			},
			blur() {
				console.log(this.scoreTF)
				let scoreTF = parseInt(this.scoreTF)
				if (scoreTF > this.maxScore) {
					this.scoreTF = this.maxScore.toString()
					uni.showToast({
						title: `可用积分${this.maxScore}`,
						icon: 'none'
					})
				}
			},
			userCoin() {
				this.$http.get(this.$api.userCoin, {}).then(res => {
					console.log(res)
					this.coinDic = res.data
					/*
						1.type:  group社群  
							groupPrice  discount_price  优惠券金额	
						2.type: activity活动
							iSVip 是否会员    vipPrice    normalPrice    discount_price  优惠券金额		 
					*/
					if (this.dataDic.type == 'activity') {
						// 三个价格
						let vipPrice = parseFloat(this.dataDic.vipPrice)
						let normalPrice = parseFloat(this.dataDic.normalPrice)
						let discount_price = parseFloat(this.dataDic.discount_price)
						// 内部成员   会员价格展示金额  不显示优惠券  可以使用积分
						// 正常用户   原价展示金额  显示优惠券   可以使用积分  优惠券>两个价格差，显示价格差   小于的话<=两个价格差, 显示优惠券金额

						if (this.dataDic.iSVip) { //VIP不能使用会员优惠   现在改成可以使用优惠券了
							let shengyuPrice = vipPrice - parseFloat(this.calculatPrice)
							let shengyuScore = (shengyuPrice * 10).toFixed(0) //剩余的金额对应的积分

							// 输入完成后最高限制的积分
							if (shengyuScore > this.coinDic.coin_now) {
								this.maxScore = this.coinDic.coin_now
							} else {
								this.maxScore = shengyuScore
							}
							// this.scoreTF = this.maxScore.toString() //智能匹配为最高可以使用的积分  该功能取消了

						} else { //非会员
							console.log(this.calculatPrice)
							let shengyuPrice = normalPrice - parseFloat(this.calculatPrice)
							console.log(shengyuPrice)
							let shengyuScore = shengyuPrice * 10 //剩余的金额对应的积分
							console.log(shengyuScore)
							console.log(this.coinDic.coin_now)
							// 输入完成后最高限制的积分							
							if (shengyuScore > this.coinDic.coin_now) {
								this.maxScore = this.coinDic.coin_now
							} else {
								this.maxScore = shengyuScore
							}
							// this.scoreTF = this.maxScore.toString() //智能匹配为最高可以使用的积分
						}
					}
				})
			},
			scoreClick() {
				this.showScore = !this.showScore
			},
			open(item) {
				this.btmShow = true
				this.hiddenScore = false
				//重置清空数据
				this.couponItem = null
				this.scoreTF = ''

				console.log(item, 'adsadsa')
				this.dataDic = item
				this.userCoin()

			},
			close() {
				this.btmShow = false
			},
			payClick() {
				this.btmShow = false
				let couponId = ''
				if (this.couponItem) {
					couponId = this.couponItem.id
				}
				this.$emit('toWechatPay', this.dataDic.itemDic, this.scoreTF, this.payMoney, couponId)
			},
			closePop() {
				this.btmShow = false
				this.$emit('closePop', '')
			}
		}
	}
</script>

<style lang="scss" scoped>
	.index {
		background: $UNI-backgroundColor;
	}

	.btmPop_wrapper {
		border-radius: 80rpx;
		overflow: hidden;
		background: white;
		min-height: 800rpx;
		padding: 30rpx;

		.name {
			color: #252525;
			font-size: 62rpx;
			font-weight: bold;
			text-align: center;
		}

		.activityTitle {
			color: #252525;
			font-weight: normal;
			font-size: 28rpx;
			text-align: center;
			margin-bottom: 30rpx;
		}

		.payBtn {
			background: linear-gradient(180deg, #F88328 0%, #FF4945 100%);
			color: white;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			border-radius: 100rpx;
			margin-top: 80rpx;
			margin-bottom: 50rpx;
		}

		.typewarn {
			padding-top: 10rpx !important;
		}

		.type1 {
			background: $UNI-backgroundColor;
			padding: 0rpx 30rpx;
			color: #252525;
			font-size: 33rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 100rpx;

			.warnAlert {
				color: #252525;
				font-size: 26rpx;
			}

			.TF {
				background: white;
				padding: 25rpx 20rpx;
				border-radius: 10rpx;
				width: 100%;
				border-radius: 80rpx;
			}

			.left {
				display: flex;
				align-items: center;

				.img {
					width: 56rpx;
					height: 56rpx;
				}

				.title {
					padding-left: 30rpx;
					color: #252525;
					font-size: 29rpx;
				}
			}

			.scoreRight {
				flex: 1;
				padding-right: 20rpx;
				text-align: right;

				.scoreTitle1 {
					color: #FE4A28;
					font-size: 26rpx;
				}

				.scoreTitle2 {
					color: #575757;
					font-size: 26rpx;
				}
			}

			.right {
				display: flex;
				align-items: center;

				.title {
					color: #FE4A28;
					font-size: 29rpx;
					padding-right: 10rpx;
				}
			}

		}

		.type2 {
			margin-top: 20rpx;
			background: white;
			padding: 20rpx 30rpx;
			color: #E02021;
			font-size: 33rpx;
		}
	}

	.arrowTransform {
		transition: 0.2s;
		transform-origin: center;
		transform: rotateZ(-90deg);
	}

	.arrowTransformReturn {
		transition: 0.2s;
		transform-origin: center;
		transform: rotateZ(0deg);
	}

	.jian {
		width: 33rpx;
		height: 33rpx;
	}
</style>